<%#
 Copyright 2016-2020 Xingwang Liao <kuoruan@gmail.com>
 Licensed to the public under the Apache License 2.0.
-%>

<%
local kcp = require "luci.model.kcptun"
local dsp = require "luci.dispatcher"

local client_file = kcp.get_config_option("client_file")
local client_version = kcp.get_kcptun_version(client_file)
local luci_version = kcp.get_luci_version()
-%>

<style type="text/css">
  .kcptun-status-node {
    margin-bottom: -18px;
  }
  .kcptun-status-item {
    margin-bottom: 18px;
    zoom: 1;
    line-height: 20px;
    font-size: 13px;
  }
  .kcptun-status-item:before, .kcptun-status-item:after {
    display: table;
    content: "";
    zoom: 1;
  }
  .kcptun-status-item-title {
    width: 180px;
    float: left;
    text-align: right;
    color: #404040;
  }
  .kcptun-status-item-value {
    margin-left: 200px;
  }
  .kcptun-status-item-value a {
    margin-left: 10px;
  }
</style>

<fieldset class="cbi-section">
  <legend><%:Running Status%></legend>
  <div class="cbi-section-node kcptun-status-node">
    <div class="kcptun-status-item">
      <div class="kcptun-status-item-title"><%:Client Status%></div>
      <div class="kcptun-status-item-value" id="_kcptun-client_status">
        <em><%:Collecting data...%></em>
      </div>
    </div>
    <div class="kcptun-status-item">
      <div class="kcptun-status-item-title"><%:Client Version%></div>
      <div class="kcptun-status-item-value">
        <%- if client_version == "" then -%>
        <em><%:Invalid Client File.%></em>
        <% else -%>
        <%=pcdata(client_version)%>
        <%- end %>
        <a href="https://github.com/xtaci/kcptun/releases/latest" target="_blank"><%:Get latest%></a>
        <a href="https://github.com/kuoruan/openwrt-kcptun/releases/latest" target="_blank"><%:Get latest%> (ipk)</a>
      </div>
    </div>
    <% if luci_version ~= "" then -%>
    <div class="kcptun-status-item">
      <div class="kcptun-status-item-title"><%:LuCI Version%></div>
      <div class="kcptun-status-item-value">
        <%=pcdata(luci_version)%>
        <a href="https://github.com/kuoruan/luci-app-kcptun/releases/latest" target="_blank"><%:Get latest%></a>
      </div>
    </div>
    <% end -%>
  </div>
</fieldset>

<script type="text/javascript">//<![CDATA[
  XHR.poll(5, '<%=dsp.build_url("admin/services/kcptun/status")%>', null,
    function(x, json) {
      if (x.status !== 200 || !json) return;
      var clientStatusElm = document.getElementById('_kcptun-client_status');
      if (clientStatusElm) {
        clientStatusElm.innerHTML = json.client ? '<%:Running%>' : '<%:Not Running%>';
      }
    }
  );
//]]></script>
